package com.tjtt.gwm.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.tjtt.gwm.bean.SheBeiWeiBaoXiangMuKu;
import com.tjtt.gwm.dao.*;
import com.tjtt.tdm.base.BaseHibernateDao;
import com.tjtt.tdm.base.PageResult;

public class SheBeiWeiBaoXiangMuKuDaoImpl extends BaseHibernateDao implements SheBeiWeiBaoXiangMuKuDao{
	
	//新增设备维保项目
	public void addWeiBaoXiangMu(SheBeiWeiBaoXiangMuKu wbxm){
		this.getHibernateTemplate().save(wbxm);
	}
	//删除设备维保项目
	public void deleteWeiBaoXiangMu(SheBeiWeiBaoXiangMuKu wbxm){
		this.getHibernateTemplate().delete(wbxm);
	}
	//修改设备维保项目
	public void updateWeiBaoXiangMu(SheBeiWeiBaoXiangMuKu wbxm){
		this.getHibernateTemplate().update(wbxm);
	}
	
	//根据id查询维保项目具体信息
	public SheBeiWeiBaoXiangMuKu queryWeiBaoXiangMuById(String id ){
		int ID=Integer.parseInt(id);
		return this.getHibernateTemplate().get(SheBeiWeiBaoXiangMuKu.class, ID);
	}
	//根据设备名称查询维保项目
	public PageResult queryWeiBaoXiangListBySheBei(Map filterMap) {
		StringBuffer countString = new StringBuffer();
		countString.append("select count(*) from GWM_SHEBEIWEIBAOXIANGMUKU wb where 1 = 1 ");
		StringBuffer queryString = new StringBuffer();	
		queryString.append("select wb.WBK_ID, wb.WBK_SHEBEINAME ,wb.WBK_WEIBAOXIANGMU, wb.WBK_WEIBAOJIZHUN,wb.WBK_CELIANGFANGFA, wb.WBK_CAOZUOYAODIAN,wb.WBK_BEIZHU " +
				" from GWM_SHEBEIWEIBAOXIANGMUKU wb  where wb.WBK_ID not in (select WBK_ID from GWM_SHEBEIWEIBAOXIANGMUKU where  WBK_WEIBAOXIANGMU is null and WBK_WEIBAOJIZHUN is null and WBK_CELIANGFANGFA is null and WBK_CAOZUOYAODIAN is null)");
		
		if(null!=filterMap && !filterMap.isEmpty()){
			if(null!=filterMap.get("epName") && !"".equals(filterMap.get("epName"))){
				queryString.append(" and wb.WBK_SHEBEINAME ='"+filterMap.get("epName")+"'");
				countString.append(" and wb.WBK_SHEBEINAME ='"+filterMap.get("epName")+"'");
			}
		}
		queryString.append(" order by wb.WBK_ID desc");
		return searchBySQL(countString.toString(),queryString.toString(),filterMap);
	}
	// 根据sql语句查询数据
	public List queryDataBySql(String sql){
		Query query = getSession().createSQLQuery(sql);
  	    List list = query.list();
  	    return list;
	}
	//判断设备名称是否存在，避免重复添加
	public boolean isExistEP(String epName){
		boolean a=false;
		String sql="select WBK_ID from GWM_SHEBEIWEIBAOXIANGMUKU where WBK_SHEBEINAME="+"'"+epName+"'";
		Query query = getSession().createSQLQuery(sql);
  	    List list = query.list();
  	    if(list!=null&&list.get(0).toString()!=null){
  	    	a=true;
  	    }
  	    return a;
	}
	//选择除设备名称为空的检测项目数据
	public List queryWbxmByNull(String epName){
		String sql="select WBK_ID from GWM_SHEBEIWEIBAOXIANGMUKU where WBK_SHEBEINAME="+"'"+epName+"' and  WBK_WEIBAOXIANGMU is null and WBK_WEIBAOJIZHUN is null and WBK_CELIANGFANGFA is null and WBK_CAOZUOYAODIAN is null";
		Query query = getSession().createSQLQuery(sql);
  	    List list = query.list();
  	    return list;
	}
	

}
